<template>
	<view class="content">
		<view class="background"></view>
		<view class="header">
			<view class="city-name-container">
				<text class="city-name">{{ cityName }}</text>
			</view>
			<view class="weather-info" style="display: flex;flex-direction: column;justify-content: center;">
				<span class="item-name">{{ info?info[0].name:"" }}：{{ info?info[0].level :""  }}级</span>
				<span class="item-level">{{ info[0].text }}</span>
			</view>
		</view>
		<view class="tips">
			<view class="tips-item" v-for="item in casts" :key="item.index">
				<view class="date-item">
					<text class="A-item-title">{{ getDayOfWeek(item.date) }}</text>
				</view>
				<view class="weather-content">
					<image class="weather-img" :src="getWeatherIcon(item.dayweather)"></image>
					<view class="weatherInfo">
						<view class="A-item">
							<image class="logo" src="/static/tem.png"></image>
							<text class="text">温度: {{ item.daytemp }}℃ ~ {{ item.nighttemp }}℃</text>
						</view>
						<view class="A-item">
							<image class="logo" src="/static/wind.png"></image>
							<text class="text">风向: {{ item.daywind }}风 ~ {{ item.nightwind }}风</text>
						</view>
						<view class="A-item">
							<image class="logo" src="/static/weather.png"></image>
							<text class="text">天气: {{ item.dayweather }} ~ {{ item.nightweather }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				latitude: 0,
				longitude: 0,
				cityName: '',
				casts: [],
				info: []
			}
		},
		methods: {
			getWeatherIcon(weather) { 
				if (weather.includes('多云')) {
					return '/static/天气-多云.png';
				} else if (weather.includes('晴')) {
					return '/static/天气-晴天.png';
				} else if (weather.includes('雨')) {
					return '/static/天气-雨天.png';
				} else if (weather.includes('阴')) {
					return '/static/天气阴天.png';
				} else {
					return '/static/天气-雪天.png';
				}
			},
			getDayOfWeek(dateString) {
				const week = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
				const date = new Date(dateString);
				return week[date.getDay()];
			},
			getLocation() {
				uni.showLoading({
					title: '加载中'
				});
				uni.getLocation({
					type: 'wgs84',
					success: (res) => {
						this.latitude = res.latitude;
						this.longitude = res.longitude;
						this.getCity();
					},
					fail: (err) => {
						console.log('位置获取失败请重试：', err);
						alert('位置获取失败,请重试');
					}
				});
			},
			getCity() {
				const key = '2718c9dcdc0e6d910398c32245385ffe';
				const location = `${this.longitude.toFixed(2)},${this.latitude.toFixed(2)}`;
				const url = `https://restapi.amap	.com/v3/geocode/regeo?key=${key}&location=${location}`;
				const urlWeather =
					`https://devapi.qweather.com/v7/indices/1d?type=1,2,3,4,5&key=6f4f8146104345faa4a564ad0325f0f2&location=${location}`;

				uni.request({
					url: url,
					method: 'GET',
					success: (res) => {
						if (res.data && res.data.regeocode) {
							this.cityName = (res.data.regeocode.addressComponent.city+'').replace('市', '');
							this.getWeatherInfo();
							this.getWeatherForecast(urlWeather);
						} else {
							console.error('获取城市名称失败', res.data);
							uni.hideLoading();
						}
					},
					fail: (error) => {
						console.error('请求失败', error);
						uni.hideLoading();
					}
				});
			},
			getWeatherInfo() {
				const key = '2718c9dcdc0e6d910398c32245385ffe';
				const url =
					`https://restapi.amap.com/v3/weather/weatherInfo?parameters&city=${this.cityName}&key=${key}&extensions=all`;

				uni.request({
					url: url,
					method: 'GET',
					success: (res) => {
						if (res.data) {
							this.casts =res.data.forecasts? res.data.forecasts[0].casts:[];
							uni.hideLoading();
						} else {
							console.error('天气信息获取失败', res.data);
						}
					},
					fail: (error) => {
						console.error('请求失败', error);
						uni.hideLoading();
					}
				});
			},
			getWeatherForecast(url) {
				uni.request({
					url: url,
					method: 'GET',
					success: (res) => {
						if (res.data) {
							this.info = res.data.daily;
						} else {
							console.error('天气预报获取失败', res.data);
						}
					},
					fail: (error) => {
						console.error('请求失败', error);
						uni.hideLoading();
					}
				});
			}
		},
		onLoad() {
			this.getLocation();
		}
	}
</script>

<style>
	.content {
		width: 100%;
	}

	.background {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-image: url('/static/cd.png');
		background-size: cover;
		filter: blur(5px);
		z-index: -1;
	}

	.header {
		display: flex;
		justify-content: space-between;
		height: 10vh;
		padding: 15px;
	}

	.city-name-container {
		width: 65%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.city-name {
		font-size: 55px;
		font-family: cursive;
		font-weight: 400;
	}

	.weather-info {
		width: 70%;
	}

	.item-name {
		font-family: cursive;
		font-weight: 600;
	}

	.item-level {
		font-family: cursive;
		font-size: 13px;
	}

	.tips { 
		display: flex;
		flex-direction: column;
		padding: 20rpx;
	}

	.tips-item {
		padding: 15px;
		border-radius: 15px;
		background-color: rgba(255, 255, 255, 0.8);
		margin-bottom: 15px;
		box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
	}

	.weather-content {
		display: flex;
		align-items: center;
		justify-content: space-evenly;
	}

	.date-item {
		margin: 5px;
	}

	.A-item {
		display: flex;
		align-items: center;
		margin: 5px;
	}

	.logo {
		height: 20rpx;
		width: 20rpx;
		margin-right: 10px;
	}

	.text {
		margin: 2px;
		font-size: small;
		font-family: serif;
	}

	.A-item-title {
		font-size: 13px;
		font-weight: bold;
	}

	.weather-img {
		width: 50px;
		height: 50px;
	}
</style>